package com.consumer1.util;


import com.consumer1.entity.ImportMovie;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class FFmpegUtils {

    /**
     * 执行指令: ffmpeg -y -i temp.mp4  -vcodec copy -acodec copy -vbsf h264_mp4toannexb temp.ts
     * @param importMovie: 视频文件的属性对象
     */
    public static void transferTS(ImportMovie importMovie) {
        // 视频文件的绝对路径
        String inputFilePath = importMovie.getMovieFilePath();
        String outputFilePath = String.format("%s%s%s",importMovie.getUuidDirPath(), File.separator,"temp.ts");

        List<String> command = new ArrayList<>();
        command.add(importMovie.getFfmpegExePath());
        command.add("-y");
        command.add("-i");
        command.add(inputFilePath);
        command.add("-vcodec");
        command.add("copy");
        command.add("-acodec");
        command.add("copy");
        command.add("-vbsf");
        command.add("h264_mp4toannexb");
        command.add(outputFilePath);

        ProcessBuilder builder = new ProcessBuilder();
        builder.command(command);
        //正常信息和错误信息合并输出
        builder.redirectErrorStream(true);
        try {
            //开始执行命令
            Process process = builder.start();

            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while (br.readLine() != null) {}

            FFmpegUtils.tsTransferM3U8(importMovie);
        } catch (IOException e) {}
    }

    /**
     * 执行指令: ffmpeg -i temp.ts -c copy -map 0 -f segment -segment_list 中文.m3u8 -segment_time 15  %3d.ts
     * @param importMovie: 视频文件的属性对象
     */
    public static void tsTransferM3U8(ImportMovie importMovie) {
        // 视频文件的绝对路径
        String fileName = importMovie.getMovieName().split("\\.")[0];
        String inputFilePath = String.format("%s%s%s",importMovie.getUuidDirPath(), File.separator,"temp.ts");

        List<String> command = new ArrayList<>();
        command.add(importMovie.getFfmpegExePath());
        command.add("-i");
        command.add(inputFilePath);
        command.add("-c");
        command.add("copy");
        command.add("-map");
        command.add("0");
        command.add("-f");
        command.add("segment");
        command.add("-segment_list");
        command.add(String.format("%s%s%s",importMovie.getUuidDirPath(), File.separator,fileName+".m3u8"));
        command.add("-segment_time");
        command.add("1");
        command.add(String.format("%s%s%s",importMovie.getUuidDirPath(), File.separator, "%3d.ts"));

        ProcessBuilder builder = new ProcessBuilder();
        builder.command(command);
        //正常信息和错误信息合并输出
        builder.redirectErrorStream(true);
        try {
            //开始执行命令
            Process process = builder.start();

            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while (br.readLine() != null) {}

        } catch (IOException e) {}
    }

}
